/*
 * Copyright 2007 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package fr.miage.client.presentation;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import fr.miage.client.metier.categorie.CategorieBusinessService;
import fr.miage.client.metier.categorie.CategorieBusinessServiceAsync;
import fr.miage.client.metier.entite.Categorie;

/**
 * Composite that represents a collection of <code>Task</code> items.
 */
public class GestCategorie extends Composite {
	private DialogBox ajoutCategorie;
	private DialogBox modifCategorie;
	
	ServiceDefTarget endpoint;
	CategorieBusinessServiceAsync categorieService;

  public GestCategorie() {
  	initDialogBox();
  	
    SimplePanel panel = new SimplePanel();
    VerticalPanel list = new VerticalPanel();
    panel.setWidget(list);
    list.add(new Button("Ajouter une catégorie", new ClickListener() {
			public void onClick(Widget sender) {
				ajoutCategorie.center();
				ajoutCategorie.show();
			}
		}));
    list.add(new Button("Modifier une catégorie"));
    initWidget(panel);
    setStyleName("mail-Tasks");
  }

	/**
	 * 
	 */
	private void initDialogBox() {
		// initialisation de la dialogBox Ajout Catégorie
		ajoutCategorie = new DialogBox();
		ajoutCategorie.ensureDebugId("cwDialogBox");
		ajoutCategorie.setText("Création de la catégorie");
		
		// Create a table to layout the content
		VerticalPanel dialogContents = new VerticalPanel();
		dialogContents.setSpacing(4);
		ajoutCategorie.setWidget(dialogContents);
		// Table dans laquelle on place les éléments
		final FlexTable flexTable = new FlexTable();
		dialogContents.add(flexTable);
		

		flexTable.setHTML(0, 0, "Nom de la catégorie : ");
		final TextBox nom = new TextBox();
		flexTable.setWidget(0, 1, nom);
		

		
		categorieService = (CategorieBusinessServiceAsync) GWT.create(CategorieBusinessService.class);
		endpoint = (ServiceDefTarget) categorieService;
		String moduleRelativeURL = GWT.getModuleBaseURL() + "categorie";
		endpoint.setServiceEntryPoint(moduleRelativeURL);
		AsyncCallback callback = new AsyncCallback(){
			public void onSuccess(Object result) {  		
				System.out.println("callback auth success");
				
				flexTable.setHTML(1, 0, "Sélectionner la catégorie Parent : ");
				final ListBox listCate = new ListBox(false);
				
				List<Categorie> laListe = (List<Categorie>) result;
				for (int i=0;i<laListe.size();i++){
					listCate.addItem(laListe.get(i).getNomCate());
				}
				flexTable.setWidget(1, 1, listCate);
			}
			
			public void onFailure(Throwable caught) {
				System.out.println("callback auth fail");
			
			}
		};

		final Button valider = new Button("Valider", new ClickListener(){
			public void onClick(Widget sender) {
			// création du proxy 
								
				categorieService = (CategorieBusinessServiceAsync) GWT.create(CategorieBusinessService.class);
				endpoint = (ServiceDefTarget) categorieService;
				String moduleRelativeURL = GWT.getModuleBaseURL() + "categorie";
				endpoint.setServiceEntryPoint(moduleRelativeURL);
				AsyncCallback callback = new AsyncCallback(){
					public void onSuccess(Object result) {  		
						System.out.println("callback inscript success");
					}
					
					public void onFailure(Throwable caught) {
						System.out.println("callback inscript fail");
					}
				};
				
				System.out.println("DEBUT DE CREATION DU CATEGORIE");
			
			}

		});
		flexTable.setWidget(2, 1, valider);
		categorieService.getAllCategorie(callback);
		Categorie categorie = new Categorie();
		categorie.setNomCate(nom.getText());
		categorieService.createCategorie(categorie, callback);
		
						
		
	}

}
